/**
 * listeners:
 * addExtraDsValues
 */
ria.buildItem = function(spec) {
    /*if(spec.extra == undefined || spec.extra._jsObjName == undefined) {
        throw "Extra property _jsObjName must be allways declared.";
    }*/
    if(spec.tpl) {
        spec.actualTpl = {};
        spec.actualTpl.tpl = 'default';
        spec.tpls = {default : spec.tpl}
    }
    //spec.tpl
    //spec.ds
    //spec.element
    //spec.actualTpl.tpl
    //spec.actualTpl.decorator
    if(spec.actualTpl) {
        if(spec.tpls[spec.actualTpl.tpl] == undefined) {
            throw 'ERROR:The informed defaultTpl "' + spec.actualTpl.tpl + '" does not exists.';
        }
    }
    if(spec.element && spec.tpl) {
        spec.element.html(spec.tpl);
    }
    if(spec.extra) {
        for(i in spec.extra) {
            spec.ds[i] = spec.extra[i];
        }
    }
    var fillTpl = function(html) { // "private method"
            spec.element.html(html)
            if(spec.element.fillTemplate.rptInstance) {
                spec.element.fillTemplate.rptInstance = null; //clear element template cache.
            }
            return this;
    };
    var ret = {
        display : function(json) {
            //[json].decorator
            //[json].tpl
            
            if(json == undefined) {
               json = spec.actualTpl;
            }
            if(json.tpl == undefined) {
                throw 'ERROR:You need to define a tpl (json.tpl) parameter.';
            }
            if(spec.extra) {
                for(i in spec.extra) {
                    spec.ds[i] = spec.extra[i];
                }
            }
            this.addExtraDsValues(spec.ds);
            if(json && json.decorator) {
                if(spec.decorators == undefined || spec.decorators[json.decorator] == undefined) {
                    throw "Decorator " + json.decorator + " is not defined";
                }
                //spec.element.fillTemplate.rptInstance = null;
                fillTpl(spec.tpls[json.tpl]);
                spec.element.fillTemplate(spec.ds);
                spec.ds._decorator_content = spec.element.html();
                spec.element.fillTemplate.rptInstance = null;
                fillTpl(spec.decorators[json.decorator]);
                spec.element.fillTemplate(spec.ds);
            }
            else {
                spec.element.fillTemplate.rptInstance = null;
                fillTpl(spec.tpls[json.tpl]);
                spec.element.fillTemplate(spec.ds);
                console.log(spec.ds);
                console.log(spec.tpl);
                console.log(spec.element);
            }
            if( this.postDisplay ) { this.postDisplay(spec, json); };
            return this;
        },
        addExtraDsValues : function(ds) {},
        setDs: function(json) {spec.ds = json.ds;return this;},
        getElement: function() {return spec.element;},
        setElement: function(newElement) {spec.element = newElement;return this;},
        getDs: function() {return spec.ds;},
        setDsVal: function( name, value) {spec.ds[name] = value;return this;},
        hide : function() {this.getElement().hide();return this;},
        show : function() {this.getElement().show();return this;},
        hasCommand : function(command) {if(spec.commands[command] != undefined) return true;}
    }
    for(i in spec.commands) {
        ret[i] = function() {
            var f = spec.commands[i];
            var s = spec;
            return function(json) {
                //console.log(f);
                //console.log(s);
                f.apply(this,[s,json]);
            }
            //.apply(this);
        }();
    }
    return ret;
};